📌 setInterval(timer, 1000);
等待一秒,呼叫timer函式
📌 let second = 0;
function timer()
{
if(second > 10)
{
second = 0;
alert(”過了十秒”);
}
second += 1;
}
在範例中當等待時間過十秒時,輸出過了十秒
📌 let second = 0;
var button = document.querySelector('#button');
function timer()
{
if(second > 10)
{
var xhr = new XMLHttpRequest();
xhr.open("GET", "/logout", true);
xhr.onreadystatechange = function ()
{
if (xhr.readyState === 4 && xhr.status === 200)
{
alert("閒置過久,自動登出")
second = 0;
window.location.href = "/gmail";
}
};
xhr.send();
}
second += 1;
console.log(second);
}
button.onclick = () =>
{
second = 0;
}
setInterval(timer, 1000);
在範例中,如果使用者未點擊按鈕時,過10秒退出系統
📌 var xhr = new XMLHttpRequest();
xhr.open("GET", "/logout", true);
xhr.onreadystatechange = function ()
{
if (xhr.readyState === 4 && xhr.status === 200)
{
alert("閒置過久,自動登出")
second = 0;
window.location.href = "/gmail";
}
};
xhr.send();
發送一個HTTP請求
/logout → session_destroy();
關閉會議後,退回根目錄
xhr.status → HTTP狀態